<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'base:car:baseClda:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.cldaCphm }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsBaseCarBaseCldaList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { baseCldaDelete, baseCldaListData } from '/@/api/base/car/baseClda';
  import { baseCldaDisable, baseCldaEnable } from '/@/api/base/car/baseClda';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('base.car.baseClda');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('车辆档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('车牌号码'),
        field: 'cldaCphm',
        component: 'Input',
      },
      {
        label: t('品牌型号'),
        field: 'cldaPpxh',
        component: 'Input',
      },
      {
        label: t('车辆类型'),
        field: 'cldaCllx',
        component: 'Input',
      },
      {
        label: t('使用性值'),
        field: 'cldaSyxz',
        component: 'Input',
      },
      {
        label: t('车辆所有人'),
        field: 'cldaClsyr',
        component: 'Input',
      },
      {
        label: t('座位数'),
        field: 'cldaZws',
        component: 'Input',
      },
      {
        label: t('默认驾驶员'),
        field: 'cldaMrjsy',
        component: 'Input',
      },
      {
        label: t('联系电话'),
        field: 'cldaLxdh',
        component: 'Input',
      },
      {
        label: t('车辆状态'),
        field: 'cldaClzt',
        component: 'Input',
      },
      {
        label: t('发动机号码'),
        field: 'cldaFdjhm',
        component: 'Input',
      },
      {
        label: t('车辆识别代号'),
        field: 'cldaClsbdh',
        component: 'Input',
      },
      {
        label: t('新能源汽车'),
        field: 'cldaXnyqc',
        component: 'Input',
      },
      {
        label: t('车体颜色'),
        field: 'cldaCtys',
        component: 'Input',
      },
      {
        label: t('车辆配置'),
        field: 'cldaClpz',
        component: 'Input',
      },
      {
        label: t('核定载人数'),
        field: 'cldaHdzrs',
        component: 'Input',
      },
      {
        label: t('整备质量'),
        field: 'cldaZbzl',
        component: 'Input',
      },
      {
        label: t('总质量'),
        field: 'cldaZzl',
        component: 'Input',
      },
      {
        label: t('变速器型式'),
        field: 'cldaBsqxs',
        component: 'Input',
      },
      {
        label: t('发动机排量'),
        field: 'cldaFdjpl',
        component: 'Input',
      },
      {
        label: t('油箱容积'),
        field: 'cldaYxrj',
        component: 'Input',
      },
      {
        label: t('保养周期'),
        field: 'cldaByzq',
        component: 'Input',
      },
      {
        label: t('销售单位'),
        field: 'cldaXsdw',
        component: 'Input',
      },
      {
        label: t('购车时间'),
        field: 'cldaGcsj',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('购车价格'),
        field: 'cldaGcjg',
        component: 'Input',
      },
      {
        label: t('注册登记日期'),
        field: 'cldaZcdjrq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('发证日期'),
        field: 'cldaFzrq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('年检日期'),
        field: 'cldaNjrq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('保险到期'),
        field: 'cldaBxdq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('限行标志'),
        field: 'cldaXxbz',
        component: 'Input',
      },
      {
        label: t('录入人员'),
        field: 'cldaLrry',
        component: 'Input',
      },
      {
        label: t('录入日期'),
        field: 'cldaLrrq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('修改人员'),
        field: 'cldaXgry',
        component: 'Input',
      },
      {
        label: t('修改日期'),
        field: 'cldaXgrq',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('车牌号码'),
      dataIndex: 'cldaCphm',
      key: 'a.clda_cphm',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('品牌型号'),
      dataIndex: 'cldaPpxh',
      key: 'a.clda_ppxh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆类型'),
      dataIndex: 'cldaCllx',
      key: 'a.clda_cllx',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('使用性值'),
      dataIndex: 'cldaSyxz',
      key: 'a.clda_syxz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆所有人'),
      dataIndex: 'cldaClsyr',
      key: 'a.clda_clsyr',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('座位数'),
      dataIndex: 'cldaZws',
      key: 'a.clda_zws',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('默认驾驶员'),
      dataIndex: 'cldaMrjsy',
      key: 'a.clda_mrjsy',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系电话'),
      dataIndex: 'cldaLxdh',
      key: 'a.clda_lxdh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆状态'),
      dataIndex: 'cldaClzt',
      key: 'a.clda_clzt',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发动机号码'),
      dataIndex: 'cldaFdjhm',
      key: 'a.clda_fdjhm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆识别代号'),
      dataIndex: 'cldaClsbdh',
      key: 'a.clda_clsbdh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('新能源汽车'),
      dataIndex: 'cldaXnyqc',
      key: 'a.clda_xnyqc',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车体颜色'),
      dataIndex: 'cldaCtys',
      key: 'a.clda_ctys',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆配置'),
      dataIndex: 'cldaClpz',
      key: 'a.clda_clpz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('核定载人数'),
      dataIndex: 'cldaHdzrs',
      key: 'a.clda_hdzrs',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('整备质量'),
      dataIndex: 'cldaZbzl',
      key: 'a.clda_zbzl',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('总质量'),
      dataIndex: 'cldaZzl',
      key: 'a.clda_zzl',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('变速器型式'),
      dataIndex: 'cldaBsqxs',
      key: 'a.clda_bsqxs',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发动机排量'),
      dataIndex: 'cldaFdjpl',
      key: 'a.clda_fdjpl',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('油箱容积'),
      dataIndex: 'cldaYxrj',
      key: 'a.clda_yxrj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('保养周期'),
      dataIndex: 'cldaByzq',
      key: 'a.clda_byzq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('销售单位'),
      dataIndex: 'cldaXsdw',
      key: 'a.clda_xsdw',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('购车时间'),
      dataIndex: 'cldaGcsj',
      key: 'a.clda_gcsj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('购车价格'),
      dataIndex: 'cldaGcjg',
      key: 'a.clda_gcjg',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('注册登记日期'),
      dataIndex: 'cldaZcdjrq',
      key: 'a.clda_zcdjrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('发证日期'),
      dataIndex: 'cldaFzrq',
      key: 'a.clda_fzrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('年检日期'),
      dataIndex: 'cldaNjrq',
      key: 'a.clda_njrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('保险到期'),
      dataIndex: 'cldaBxdq',
      key: 'a.clda_bxdq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('限行标志'),
      dataIndex: 'cldaXxbz',
      key: 'a.clda_xxbz',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('录入人员'),
      dataIndex: 'cldaLrry',
      key: 'a.clda_lrry',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('录入日期'),
      dataIndex: 'cldaLrrq',
      key: 'a.clda_lrrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('修改人员'),
      dataIndex: 'cldaXgry',
      key: 'a.clda_xgry',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('修改日期'),
      dataIndex: 'cldaXgrq',
      key: 'a.clda_xgrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑车辆档案'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'base:car:baseClda:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用车辆档案'),
        popConfirm: {
          title: t('是否确认停用车辆档案'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'base:car:baseClda:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用车辆档案'),
        popConfirm: {
          title: t('是否确认启用车辆档案'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'base:car:baseClda:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除车辆档案'),
        popConfirm: {
          title: t('是否确认删除车辆档案'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'base:car:baseClda:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: baseCldaListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id };
    const res = await baseCldaDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id };
    const res = await baseCldaEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await baseCldaDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
